package com.jkoss.wine.merchant.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jkoss.common.util.UuidUtil;
import com.jkoss.wine.merchant.entity.MerchantProductNorm;
import com.jkoss.wine.merchant.mapper.MerchantProductNormMapper;
import com.jkoss.wine.merchant.service.IMerchantProductNormService;
import com.jkoss.wine.shop.entity.Product;
import com.jkoss.wine.shop.entity.ProductInventory;
import com.jkoss.wine.shop.entity.ProductNorm;
import com.jkoss.wine.shop.entity.Shop;
import com.jkoss.wine.shop.mapper.MerchantCommonTypeMapper;
import com.jkoss.wine.shop.mapper.ProductInventoryMapper;
import com.jkoss.wine.shop.mapper.ProductMapper;
import com.jkoss.wine.shop.mapper.ProductNormMapper;
import com.jkoss.wine.shop.mapper.ShopMapper;

/**
 * 服务实现类
 * 
 * @Author Ning
 * @Version 1.0, 2018-09-28
 * @See
 * @Since com.jkoss.mengal.merchant.service.impl
 * @Description: TODO
 */
@Service
public class MerchantProductNormServiceImpl extends ServiceImpl<MerchantProductNormMapper, MerchantProductNorm> implements IMerchantProductNormService {

	@Autowired
	private MerchantCommonTypeMapper merchantCommonTypeMapper;

	@Autowired
	private ProductNormMapper productNormMapper;

	@Autowired
	private MerchantProductNormMapper merchantProductNormMapper;

	@Autowired
	private ShopMapper shopMapper;

	@Autowired
	private ProductMapper productMapper;

	@Autowired
	private ProductInventoryMapper productInventoryMapper;

	@Override
	public boolean insertMerchantProductNorm(MerchantProductNorm vo) throws Exception {
		String normName = vo.getNormName();
		String merchantProductId = vo.getMerchantProductId();

		// Example example = new Example(ProductNorm.class);
		// Example.Criteria criteria = example.createCriteria();
		// criteria.andEqualTo(merchantProductNorm);

		Wrapper wrapperNormNameCount = Condition.create().eq("norm_name", vo.getNormName());

		int i = merchantProductNormMapper.selectCount(wrapperNormNameCount);
		if (i > 0) {
			throw new Exception("名称已经存在");
		}
		Wrapper wrapperNumCount = Condition.create().eq("product_num", vo.getProductNum());
		if (merchantProductNormMapper.selectCount(wrapperNumCount) > 0) {
			throw new Exception("该条形码已经被使用");
		}
		if (!retBool(merchantProductNormMapper.insert(vo))) {
			throw new Exception("添加失败");
		}

		List<Shop> shops = shopMapper.selectList(null);
		List<ProductNorm> shopProductNorm = new ArrayList<>();

		// region 获取各个店铺下的productID
		Wrapper wrapperProduc = Condition.create().eq("parent_id", vo.getMerchantProductId());
		List<Product> products = productMapper.selectList(wrapperProduc);
		Map<String, String> productIds = new HashMap<>();
		for (Product product : products) {
			productIds.put(product.getShopId(), product.getProductId());
		}
		// endregion
		for (Shop shop : shops) {
			String productId = productIds.get(shop.getShopId());
			if (productId != null) {
				ProductNorm productNorm = new ProductNorm();
				BeanUtils.copyProperties(productNorm, vo);
				productNorm.setSorting(0);
				productNorm.setProductNormId(UuidUtil.getUUID());
				productNorm.setShopId(shop.getShopId());
				productNorm.setProductId(productId);
				productNorm.setDel(0);
				productNorm.setNormPrice(vo.getSuggestedPrice());
				productNorm.setParentId(vo.getMerchantProductNormId());
				productNorm.setNormPicUrl(vo.getNormPicUrl());
				shopProductNorm.add(productNorm);
			}
		}
		if (shopProductNorm.size() > 0) {
			if (!retBool(productNormMapper.insertBatch(shopProductNorm))) {
				throw new Exception("添加失败");
			}
		}

		// region 添加库存
		List<ProductInventory> productInventories = new ArrayList<>();
		for (ProductNorm productNorm : shopProductNorm) {
			ProductInventory temp = new ProductInventory();
			temp.setProductInventoryId(UuidUtil.getUUID());
			temp.setProductId(productNorm.getProductId());
			temp.setProductNormId(productNorm.getProductNormId());
			temp.setShopId(productNorm.getShopId());
			temp.setInventoryNum(null);
			temp.setBasketId(null);
			temp.setInventory(0);
			temp.setType(0);
			temp.setOperator(vo.getOperator());
			temp.setAddTime(new Date());
			temp.setRemark("创建商品自动入库");
			productInventories.add(temp);
		}
		if (productInventories.size() > 0) {
			if (!retBool(productInventoryMapper.insertBatch(productInventories))) {
				throw new Exception("添加失败");
			}
		}
		// endregion

		return true;
	}

	@Override
	public boolean updateProductNorm(MerchantProductNorm merchantProductNorm) throws Exception {

		Wrapper wrapper1 = Condition.create().eq("norm_name", merchantProductNorm.getNormName()).eq("merchant_product_id", merchantProductNorm.getMerchantProductId()).ne("merchant_product_norm_id", merchantProductNorm.getMerchantProductNormId());

		int j = merchantProductNormMapper.selectCount(wrapper1);
		if (j > 0) {
			throw new Exception("规格已存在！");
		}

		int i = merchantProductNormMapper.updateById(merchantProductNorm);
		if (i != 1) {
			throw new Exception("更新失败:找不到该条记录");
		}

		ProductNorm productNorm = new ProductNorm();
		Wrapper wrapper2 = Condition.create().eq("parent_id", merchantProductNorm.getMerchantProductNormId());
		productNorm.setParentId(merchantProductNorm.getMerchantProductNormId());
		BeanUtils.copyProperties(productNorm, merchantProductNorm);
		if (!retBool(productNormMapper.update(productNorm, wrapper2))) {
			throw new Exception("更新失败");
		}
		return true;
	}

	@Override
	public boolean deleteMerchantProductNorm(String[] id) throws Exception {

		// 逻辑删除总店的通用类型
		for (String string : id) {
			MerchantProductNorm merchantProductNorm = new MerchantProductNorm();
			merchantProductNorm.setMerchantProductNormId(string);
			// 是否删除：0-未删除、1-已删除、2-不能删除
			merchantProductNorm.setDel(1);
			baseMapper.updateById(merchantProductNorm);
		}
		// 删除门店的通用类型
		for (String string : id) {
			ProductNorm productNorm = new ProductNorm();
			productNorm.setParentId(string);
			// 是否删除：0-未删除、1-已删除、2-不能删除
			productNorm.setDel(1);
			Wrapper wrapper = Condition.create().eq("parent_id", string);
			productNormMapper.update(productNorm, wrapper);
		}
		return true;
	}

}
